// pages/tools/yunchan/cljl.js
var _$ = require("../../../common/wxjs/wx_ajax");
const url = "yc_zwjl";

Page({
    /**
     * 页面的初始数据
     */
    data: {
        data:'',
        uesrtoken:null,
        formdata: {
            "wyfs": 0,
            "br_time": '',
            "ydlx": 0,
            "sx": 0,
            "fmyc": 0,
            "chtz": '',
            "yjhf": 0,
            "hf_data": "请点击选择日期",
            "by": 0,
            "byff": 0,
            "chqk": 0,
            "ft": 0,
            "fr": 0,
            "ydfmw": 0,
            "rfhzrt": 0,
            "skyh": 0,
            "qxdl": 0,
            "wt": "",
            "ysjy": "",
            "mmyexd": "",
            "bbyexd": "",
        }
    },
    // 提交表单
    formSubmit: function(e) {
        var that = this;
        var userdata = e.detail.value;
        userdata = JSON.stringify(userdata);
        var litToken = that.data.uesrtoken;
        var type = 1;
        var methods = "POST";
        var header = "application/x-www-form-urlencoded";
        var dataobj = { type, userdata,litToken };
        wx.showToast({
            title: '正在提交',
            icon: 'loading',
            duration: 2000
        })
        _$.sendRrquest(url, methods, dataobj, header)
            .then(function(response) {
              console.log(response)
                if (response.data.status == 200 ) {
                    wx.showToast({
                        title: '提交成功',
                        icon: 'success',
                        duration: 2000
                    });
                    setTimeout(function() {
                        wx.hideToast()
                    }, 2000)
                } else {
                    wx.showToast({
                        title: '数据未修改',
                        icon: 'loading',
                        duration: 2000
                    });
                    setTimeout(function() {
                        wx.hideToast()
                    }, 2000)
                }
            }, function(error) {
                wx.showToast({
                    title: '提交失败',
                    duration: 2000
                });
                setTimeout(function() {
                    wx.hideToast()
                }, 2000)
                console.log(error);
            });
    },
    // 请求数据
    getdata: function(val) {
        var that = this;
        var type = 0;
        var litToken = val;
        var dataobj = { type, litToken };
        var methods = "GET";
        var header = "application/json";
        wx.showLoading({
            title: '加载中',
        })
        _$.sendRrquest(url, methods, dataobj, header)
            .then(function(response) {
                setTimeout(function() {
                    wx.hideLoading()
                }, 1000)
                console.log(response)
                that.setData({
                    formdata: response.data.data.res
                });
                if (response.data.data.res.hf_data == "") {
                  that.setData({
                    "formdata.hf_data": "请点击选择日期"
                  });
                }
            }, function(error) {
                setTimeout(function() {
                    wx.hideLoading()
                }, 2000)
                wx.showToast({
                    title: '网络错误，请稍后再试',
                    duration: 2000
                })
                setTimeout(function() {
                    wx.hideToast()
                }, 2000)
                console.log(error);
            });
    },
    // 按钮切换
    btnselect: function(e) {
        var that = this;
        that.setData({
            "formdata.wyfs": e.target.dataset.wyfs,
            "formdata.ydlx": e.target.dataset.ydlx,
            "formdata.sx": e.target.dataset.sx,
            "formdata.fmyc": e.target.dataset.fmyc,
            "formdata.yjhf": e.target.dataset.yjhf,
            "formdata.by": e.target.dataset.by,
            "formdata.byff": e.target.dataset.byff,
            "formdata.chqk": e.target.dataset.chqk,
            "formdata.ft": e.target.dataset.ft,
            "formdata.fr": e.target.dataset.fr,
            "formdata.ydfmw": e.target.dataset.ydfmw,
            "formdata.rfhzrt": e.target.dataset.rfhzrt,
            "formdata.skyh": e.target.dataset.skyh,
            "formdata.qxdl": e.target.dataset.qxdl,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
       var that = this
      wx.getStorage({
        key: 'token',
        success: function (res) {
          that.setData({
            uesrtoken: res.data
          })
          that.getdata(res.data)
        }
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    bindDateChange: function(e) {
        this.setData({
            "formdata.hf_data": e.detail.value
        })
    },
    todyDate: function() {
        var that = this;
        var date = util.formatTime(new Date());
        var dateYear = date.substring(0, 11);
        var dateTime = date.substring(11, date.length - 3);
        console.log(dateTime)
        this.setData({
            date: dateYear,
            time: dateTime
        })
    }
})